package com.tx.utils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

/**
 * @Author: tcx
 * @description: MD5加密
 * @Date: 2020/2/6
 **/
public class StringUtil {
    private final static Log log = LogFactory.getLog(StringUtil.class);

    private static MessageDigest digest = null;

    /**
     * 对字符串进行MD5加密
     * @param data
     * @return
     */
    public static String encode(String data) {
        if (digest == null)
            try {
                digest = MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException nsae) {
                log.error("Failed to load the MD5 MessageDigest."
                        + " We will be unable to function normally.");
                nsae.printStackTrace();
            }
        digest.update(data.getBytes());
        return encodeHex(digest.digest());
    }

    private static String encodeHex(byte bytes[]) {
        StringBuffer buf = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            if ((bytes[i] & 0xff) < 16)
                buf.append("0");
            buf.append(Long.toString(bytes[i] & 0xff, 16));
        }
        return buf.toString().toUpperCase();
    }

    /**
     * 生成16位随机数
     */
    public static String salt(){
        Random r = new Random();
        StringBuilder sb = new StringBuilder(16);
        sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));
        int len = sb.length();
        if (len < 16) {
            for (int i = 0; i < 16 - len; i++) {
                sb.append("0");
            }
        }
        String salt = sb.toString();
        return  salt;
    }

    /**
     * 判断是否是空
     */
    public static boolean isEmpty(String str) {
        if (str == null || "".equals(str.trim())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断是否不是空
     */
    public static boolean isNotEmpty(String str) {
        if ((str != null) && !"".equals(str.trim())) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 格式化模糊查询
     */
    public static String formatLike(String str) {
        if (isNotEmpty(str)) {
            return "%" + str + "%";
        } else {
            return null;
        }
    }
    /**
     * 结束日期拼接
     */
    public static String formatEndTime(String str) {
        if (isNotEmpty(str)) {
            return  str + " 23:59:59";
        } else {
            return null;
        }
    }
    /**
     * 截取第一个冒号后面的字符串
     */
    public static String getContent(String str){
        String str3=str.substring(0, str.indexOf(":"));
        String content=str.substring(str3.length()+1, str.length());
        return content;
    }

    /*public static void main(String[] args) throws InterruptedException {
        *//*System.out.println(StringUtil.encode("082785"));
        Thread.sleep(100000);
        System.out.println(StringUtil.encode("082785"));*//*
        System.out.println(StringUtil.getContent("@adminnick:回复测试1"));
    }*/
}
